package com.aistar.config;

import com.aistar.filter.AutoLoginFilter;
import com.aistar.service.CustomerService;
import com.aistar.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
public class FilterConfig {
    private final CustomerService customerService;
    private final MessageService messageService;

    @Autowired
    public FilterConfig(CustomerService customerService,
                        MessageService messageService) {
        this.customerService = customerService;
        this.messageService = messageService;
    }

    @Bean
    public FilterRegistrationBean<Filter> testFilterRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new AutoLoginFilter(customerService, messageService));
        registration.addUrlPatterns("/*");//配置过滤路径
        registration.setName("autoLoginFilter");//设置filter名称
        registration.setOrder(1);//请求中过滤器执行的先后顺序，值越小越先执行
        return registration;
    }
}
